package dao;

/**
 * @author Renata
 */
import java.sql.ResultSet;
import java.sql.Statement;
import model.Usuario;

public class UsuarioDao extends Dao {

    public void create(Usuario u) throws Exception {

        open();

        stmt = con.prepareStatement("INSERT INTO USUARIO(nome, senha , login, tipo) VALUES(?,md5(?),?,?)",Statement.RETURN_GENERATED_KEYS);
            

        stmt.setString(1, u.getNome());
        stmt.setString(2, u.getSenha());
        stmt.setString(3, u.getLogin());
        stmt.setInt(4, u.getTipo());

        stmt.execute();
        
        close();
    }
    
    public void inserirNoRanking(Usuario u) throws Exception{
        open();
        stmt = con.prepareStatement("INSERT INTO ranking(id_user) VALUES(?)",Statement.RETURN_GENERATED_KEYS);
        
        stmt.setInt(1, (int)u.getId());
     
        stmt.executeUpdate();
        
        close();
    }

    public void inserirNasEstatisticas(Usuario u) throws Exception {
         open();
        stmt = con.prepareStatement("INSERT INTO estatisticasIndividuais(id_user) VALUES(?)",Statement.RETURN_GENERATED_KEYS);
       
        stmt.setInt(1, (int)u.getId());
     
        stmt.executeUpdate();
        close();
    }
    
    public void atualizaId(Usuario u) throws Exception{
        ResultSet rs = null;
        int id = 0;
        open();
        stmt = con.prepareStatement("SELECT max(id) FROM usuario");
      
        rs = stmt.executeQuery();
        
        while(rs.next()){
            id = rs.getInt(1);
        }
        
        u.setId(id);
        close();
        
    }
}
